package com.jams.goodox.activity.wallet;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangzhou.jin.customview.TitleView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.listen.DialogActionListener;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.DataCacheManager;
import com.jams.goodox.utils.MD5Utils;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.EditTextWithDel;
import com.jams.goodox.widget.PromptBuilderDialog;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Jin on 2016/12/26.
 * Description 设置（重置）支付密码 page2
 */
public class SetPayPassword2Activity extends BaseActivity {

    @Bind(R.id.title_view)
    TitleView mTitleView;
    @Bind(R.id.mid_content)
    TextView mMidContent;
    @Bind(R.id.password1)
    EditTextWithDel mPassword1;
    @Bind(R.id.submit)
    TextView mSubmit;
    @Bind(R.id.mask)
    RelativeLayout mask;


    // 用于设置成功后回退到的页面  默认是个人设置页
    public static final int COME_ChoosePayWayActivity = 1;
    public static final int COME_MineWalletActivity = 2;
    public static final int COME_MineWalletCardActivity = 3;

    // 密码框长度输入限制
    public static final int INPUT_LENGTH_LIMIT = 6;


    @Override
    protected int getLayoutId() {
        return R.layout.set_pay_password2;
    }

    @Override
    protected void init() {
        setTitleVisibility(false);
        if (SePref.isSetPayPwd()) {
            mTitleView.setTitle("重置支付密码");
            mMidContent.setText("重置支付密码");
        }
        mTitleView.injectLeftImg(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                close();
            }
        });
        mPassword1.addTextChangedListener(new CustomChangeListener());
    }

    /**
     * 请求接口设置密码
     *
     * @param pwd password
     */
    private void setPassword(String pwd) {

        mask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 空方法覆盖底层的点击事件
            }
        });
        mask.setVisibility(View.VISIBLE);

        String phone = DataCacheManager.getInstance().getUser().getMobileNum();
        enqueue(createCallService(MineService.class).setPayPwd(getIntent().getStringExtra("code"), MD5Utils.md5(pwd), phone), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                ToastUtil.show(mContext, "设置成功");
                SePref.setHasPayPwd(true);
                mask.setVisibility(View.GONE);
                finish();
            }

            @Override
            public void onError(String msg) {
                mask.setVisibility(View.GONE);
            }
        });
    }

    @OnClick(R.id.submit)
    public void onClick() {
        if (hasCould()) {
            String pwd1 = mPassword1.getText().toString();
            setPassword(pwd1);
        }else{
           ToastUtil.show("请输入6位数字支付密码");
        }
    }

    /**
     * 2个密码框都6个了没
     */
    private boolean hasCould() {
        return mPassword1.getText().length() == INPUT_LENGTH_LIMIT;
    }

    class CustomChangeListener implements TextWatcher {

        Drawable drawable1, drawable2;

        public CustomChangeListener() {
            drawable1 = ContextCompat.getDrawable(mContext, R.drawable.bg_cancel_theme);
            drawable2 = ContextCompat.getDrawable(mContext, R.drawable.bg_cancel_theme);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mSubmit.setBackground(hasCould() ? drawable1 : drawable2);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public void onBackPressed() {
        close();
    }

    private void close() {
        new PromptBuilderDialog(this)
                .setContent("是否放弃设置支付密码？")
                .setPositive("是")
                .setNegative("否")
                .setActionClickListener(new DialogActionListener() {
                    @Override
                    public void confirm(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }

                    @Override
                    public void cancel(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }
}
